/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						 dialog_load_game.cpp

	$Header: /resource_editor/dialog_load_game.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "dialog_load_game.h"

#include <fstream>
#include "bitmap_group.h"
#include "bitmap_group_cache.h"
#include "bound_handler.h"
#include "button.h"
#include "button_cache.h"
#include "external_string.h"
#include "interface_help.h"

static t_bitmap_group_cache		k_bitmaps( "dialog.load_game" );
static t_external_string const  k_text_load_game( "load_game.dialog" );
static t_external_string const	k_text_load( "Load.dialog" );
extern t_button_cache			g_large_button;

// ------------------------------------------------------------------
// save game dialog
// ------------------------------------------------------------------
t_dialog_load_game::t_dialog_load_game( t_window* parent )
                  : t_dialog_file( k_bitmaps.get(), parent )
{
	t_screen_rect  rect;
	t_screen_point point;
	t_help_block const&		help = get_help_block( "load_game" );

	// add load button
	point = m_bitmaps->find( "load_location" )->get_rect().top_left() + m_background_origin;
	m_load_button = new t_button( g_large_button.get(), point, m_background, k_text_load );
	m_load_button->set_click_handler( bound_handler( *this, &t_dialog_load_game::load_click ) );
	m_load_button->enable( false );
	set_help( m_load_button, help, "load" );

	// create title
	create_title( k_text_load_game );
	read_directory( "games" );
}

// ------------------------------------------------------------------
// load game dialog
// ------------------------------------------------------------------
std::string t_dialog_load_game::get_file_name() const
{
	if (m_selected_item >= (int)m_files.size())
		return "";

	return m_files[m_selected_item].file_name;
}


// ------------------------------------------------------------------
// load game dialog
// ------------------------------------------------------------------
std::string t_dialog_load_game::get_full_name() const
{
	if (m_selected_item >= m_files.size())
		return "";

	return m_folder + m_files[m_selected_item].file_name + ".h4s";
}

// ------------------------------------------------------------------
// load game dialog
// ------------------------------------------------------------------
t_saved_game_header const& t_dialog_load_game::get_header() const
{
	assert( m_selected_item < m_files.size());
	return m_files[m_selected_item].header;
}



// ------------------------------------------------------------------
// save game dialog
// ------------------------------------------------------------------
void t_dialog_load_game::select_file( t_file_data& data )
{
	m_load_button->enable( !data.is_folder );
}

// ------------------------------------------------------------------
// save game dialog
// ------------------------------------------------------------------
void t_dialog_load_game::finish()
{
	m_return_value = true;
	close();
}

// ------------------------------------------------------------------
// save game dialog
// ------------------------------------------------------------------
void t_dialog_load_game::load_click( t_button* )
{
	finish();
}

